package cn.hp.controller;

import cn.hp.entity.Dite;
import cn.hp.service.DiteService;
import cn.hp.util.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dite")
@Api(value = "/dite",tags = "字典管理")
public class DiteController {

    @Autowired
    private DiteService diteService;

    @GetMapping
    @ApiOperation(value = "查询",notes = "查询所有字典")
    public Result find(Dite dite){
        List<Dite> diteList=diteService.selectDite(dite);
        return Result.success(diteList);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询",notes = "分页查询所有字典")
    public Result page(Dite dite,@RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "10") Integer limit){
        PageHelper.startPage(page,limit);
        PageInfo<Dite>PageInfo=diteService.selectDite(dite,page,limit);
        return Result.success(PageInfo);
    }
    @PostMapping
    @ApiOperation(value = "新增",notes = "新增字典")
    private Result insert(@RequestBody Dite dite){
        int i= diteService.insertDite(dite);
        return i>0 ? Result.success("添加成功"):Result.error("添加失败");
    }
    @PutMapping
    @ApiOperation(value = "修改",notes = "修改字典")
    private Result update(@RequestBody Dite dite){
        int i=diteService.updateDite(dite);
        return i>0 ? Result.success("修改成功"):Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "删除字典")
     private Result delete(@PathVariable Integer id ){
        int i=diteService.deleteDiteById(id);
        return i>0 ? Result.success("删除成功"):Result.error("删除失败");
    }


}
